package com.ruoyi.common.utils;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.mpatric.mp3agic.ID3v2;
import com.mpatric.mp3agic.Mp3File;
import com.ruoyi.common.utils.vo.AlbumInfo;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.io.File;

/**
 * @author Licl
 */
@Slf4j
public class Mp3AgicUtil {

    @SneakyThrows
    private static void aaa(String path){
        Mp3File mp3file = new Mp3File(path);
        System.out.println("Length of this mp3 is: " + mp3file.getLengthInSeconds() + " seconds");
        System.out.println("Bitrate: " + mp3file.getBitrate() + " kbps " + (mp3file.isVbr() ? "(VBR)" : "(CBR)"));
        System.out.println("Sample rate: " + mp3file.getSampleRate() + " Hz");
        System.out.println("Has ID3v1 tag?: " + (mp3file.hasId3v1Tag() ? "YES" : "NO"));
        System.out.println("Has ID3v2 tag?: " + (mp3file.hasId3v2Tag() ? "YES" : "NO"));
        System.out.println("Has custom tag?: " + (mp3file.hasCustomTag() ? "YES" : "NO"));
        System.out.println(mp3file.getBitrate());
        System.out.println(mp3file.getLengthInSeconds());
        System.out.println(mp3file.getVersion());
        System.out.println(mp3file.getSampleRate());
        System.out.println(mp3file.getEmphasis());
        if (mp3file.hasId3v2Tag()) {
            ID3v2 id3v2Tag = mp3file.getId3v2Tag();
            System.out.println("Track: " + id3v2Tag.getTrack());
            System.out.println("Artist: " + id3v2Tag.getArtist());
            System.out.println("Title: " + id3v2Tag.getTitle());
            System.out.println("Album: " + id3v2Tag.getAlbum());
            System.out.println("Year: " + id3v2Tag.getYear());
            System.out.println("Genre: " + id3v2Tag.getGenre() + " (" + id3v2Tag.getGenreDescription() + ")");
            System.out.println("Comment: " + id3v2Tag.getComment());
            System.out.println("Lyrics: " + id3v2Tag.getLyrics());
            System.out.println("Composer: " + id3v2Tag.getComposer());
            System.out.println("Publisher: " + id3v2Tag.getPublisher());
            System.out.println("Original artist: " + id3v2Tag.getOriginalArtist());
            System.out.println("Album artist: " + id3v2Tag.getAlbumArtist());
            System.out.println("Copyright: " + id3v2Tag.getCopyright());
            System.out.println("URL: " + id3v2Tag.getUrl());
            System.out.println("Encoder: " + id3v2Tag.getEncoder());
            byte[] albumImageData = id3v2Tag.getAlbumImage();
            if (albumImageData != null) {
                System.out.println("Have album image data, length: " + albumImageData.length + " bytes");
                System.out.println("Album image mime type: " + id3v2Tag.getAlbumImageMimeType());
            }
        }
        if (mp3file.hasId3v2Tag()) {
            ID3v2 id3v2Tag = mp3file.getId3v2Tag();
            byte[] imageData = id3v2Tag.getAlbumImage();
            if (imageData != null) {
                FileUtil.writeBytes(imageData, "E:/Users/Licl/Music/img.jpeg");
            }
        }
    }

    /**
     * 获取音频时长
     *
     * @param path 音频文件地址
     * @return  返回音频文件时长
     */
    @SneakyThrows
    public static long duration(String path){
        Mp3File mp3File = new Mp3File(path);
        return mp3File.getLengthInSeconds();
    }

    /**
     * 根据歌曲信息提取歌曲的专辑图片
     *
     * @param path  歌曲信息
     * @return      生成好的专辑图片
     */
    @SneakyThrows
    public static File buildAlbumPicture(String path){
        if ("audio/mpeg".equals(FileUtil.getMimeType(path))){
            Mp3File mp3File = new Mp3File(path);
            if (mp3File.hasId3v2Tag()) {
                ID3v2 id3v2Tag = mp3File.getId3v2Tag();
                String album = id3v2Tag.getAlbum();
                byte[] albumImageData = id3v2Tag.getAlbumImage();
                if (albumImageData != null) {
                    String name = FileUtil.getName(path);
                    String replace = StrUtil.replace(path, name, "");
                    File file = new File(replace + album + "-albumImage.jpeg");
                    if (!file.isFile()) {
                        try {
                            FileUtil.writeBytes(albumImageData, file);
                        }catch (Exception e){
                            log.error("文件找不到:" + file);
                            e.printStackTrace();
                        }
                        return file;
                    }else {
                        return file;
                    }
                }
            }
        }
        return null;
    }

    /**
     * 获得歌曲的专辑
     *
     * @param path 歌曲路径
     * @return  获得的专辑
     */
    @SneakyThrows
    public static String album(String path){
        if ("audio/mpeg".equals(FileUtil.getMimeType(path))){
            Mp3File mp3File = new Mp3File(path);
            if (mp3File.hasId3v2Tag()) {
                ID3v2 id3v2Tag = mp3File.getId3v2Tag();
                return id3v2Tag.getAlbum();
            }
        }
        return null;
    }

    /**
     * 获取MP3的标题
     *
     * @param path 歌曲路径
     * @return  获得的专辑
     */
    @SneakyThrows
    public static String getMp3Title(String path){
        if ("audio/mpeg".equals(FileUtil.getMimeType(path))){
            Mp3File mp3File = new Mp3File(path);
            if (mp3File.hasId3v2Tag()) {
                ID3v2 id3v2Tag = mp3File.getId3v2Tag();
                return id3v2Tag.getTitle();
            }
        }
        return null;
    }

    /**
     * 获得艺术季
     *
     * @param path 歌曲路径
     * @return 返回的艺术家
     */
    @SneakyThrows
    public static String getArtist(String path){
        if ("audio/mpeg".equals(FileUtil.getMimeType(path))){
            Mp3File mp3File = new Mp3File(path);
            if (mp3File.hasId3v2Tag()) {
                ID3v2 id3v2Tag = mp3File.getId3v2Tag();
                return id3v2Tag.getTitle();
            }
        }
        return null;
    }

    @SneakyThrows
    public static AlbumInfo buildAlbumInfo(String path){
        if ("audio/mpeg".equals(FileUtil.getMimeType(path))){
            Mp3File mp3file = new Mp3File(path);
            if (mp3file.hasId3v2Tag()) {
                ID3v2 id3v2Tag = mp3file.getId3v2Tag();
                return AlbumInfo.builder()
                        .album(id3v2Tag.getAlbum())
                        .artist(id3v2Tag.getArtist())
                        .albumImage(buildAlbumPicture(path))
                        .build();
            }
        }
        return null;
    }



}
